/**
  基于右侧全屏组件
*/
<template>
  <transition
    name="dialog-fade"
    @after-enter="afterEnter"
    @after-leave="afterLeave"
  >
    <div
      v-if="visible"
      :style="{ marginLeft: mainMarginLeft }"
      class="wx-full"
    >
      <div role="dialog" class="main-context" aria-modal="true">
        <div class="el-dialog__header">
          <slot name="title">
            <span class="title el-dialog__title">{{ title }}</span>
          </slot>
          <div class="tools">
            <slot name="tools" />
          </div>
          <button
            type="button"
            class="el-dialog__headerbtn"
            aria-label="Close"
            @click="() => $emit('update:visible', false)"
          >
            <i class="el-dialog__close el-icon el-icon-close" />
          </button>
        </div>
        <div ref="wxFull" class="context">
          <slot />
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'WxFull',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '标题'
    }
  },
  data() {
    return {
      fillWidth: '0',
      mainMarginLeft: '210px'
    }
  },
  computed: {
    ...mapGetters(['sidebar'])
  },
  watch: {
    'sidebar.opened'(val) {
      this.$nextTick(() => {
        this.mainMarginLeft = '170px'
      })
    },
    visible(val) {
      if (val) {
        this.$nextTick(() => {
          // 返回到顶部
          this.$refs.wxFull.scrollTo(0, 0)
          this.mainMarginLeft = '170px'
        })
      }
      this.toggleFull(val)
      // fix scroll bug
      document.querySelector('body').style.overflow = val ? 'hidden' : 'unset'
    }
  },
  beforeDestroy() {
    document.querySelector('body').style.overflow = 'unset'
    this.toggleFull(false)
  },
  methods: {
    ...mapActions({
      toggleFull: 'app/toggleFull'
    }),
    afterEnter() {
      this.$emit('opened')
    },
    afterLeave() {
      this.$emit('closed')
    }
  }
}
</script>

<style lang="scss" scoped>
  @import '@/styles/variables.scss';
  .wx-full {
    min-height: calc(100vh - 50px);
    margin-top: 50px;
    transition: margin-left .28s;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 10;
    box-shadow: 4px 1px 5px #333333;
    .main-context {
      height: 100%;
      background: #ffffff;
      .el-dialog__header {
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #cecece;
        .tools {
          margin-right: 40px;
        }
      }
      .el-dialog__headerbtn {
        font-size: 24px;
      }
      .context {
        height: calc(100% - 64px);
        overflow: auto;
        padding: 20px;
      }
    }
  }
</style>
